//
//  MHComicIntroduct.m
//  ManHua
//
//  Created by 赵永泉 on 15/10/26.
//  Copyright © 2015年 蓝欧科技. All rights reserved.
//

#import "MHComicIntroduct.h"
#import <AFNetworking.h>
#import <SDWebImageManager.h>

@implementation MHComicIntroduct
- (void)setValue:(id)value forKey:(NSString *)key
{
    [super setValue:value forKey:key];
    if ([key isEqualToString:@"desc"]) {
        self.BookDescription = value;
    }
    if ([key isEqualToString:@"dt_updated"]) {
        self.BookUpdateDate = value;
    }
    if ([key isEqualToString:@"title"]) {
        self.BookName = value;
    }
    if ([key isEqualToString:@"image_set_id"]) {
        [[AFHTTPRequestOperationManager manager] GET:[NSString stringWithFormat:@"https://pi.comikon.net/imagegroup/%@/autopick/", value] parameters:[NSDictionary dictionaryWithObjects:@[@"json", @"440", @"both", @"640"] forKeys:@[@"format", @"height", @"match", @"width"]] success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
            self.BookIconOtherURL = responseObject[@"image_url"];
            self.imageSize = CGSizeMake([responseObject[@"width"] floatValue], [responseObject[@"height"] floatValue]);
            [[NSNotificationCenter defaultCenter]postNotificationName:@"imageUrl" object:nil userInfo:[NSDictionary dictionaryWithObjects:@[self.BookIconOtherURL, [NSNumber numberWithInteger:self.seq]] forKeys:@[@"url", @"index"]]];
        } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
            NSLog(@"%@", error.localizedDescription);
        }];
    }
    if ([key isEqualToString:@"objects"]) {
        self.BookID = [value[@"meta_id"] integerValue];
    }
    
    if ([key isEqualToString:@"comic_author"]) {
        self.BookAuthor = value;
    }
    if ([key isEqualToString:@"comic_name"]) {
        self.BookName = value;
    }
    if ([key isEqualToString:@"description"]) {
        self.BookDescription = value;
    }
    if ([key isEqualToString:@"resources"]) {
        self.resource_id = [value[0][@"resource_id"] integerValue];
    }
    if ([key isEqualToString:@"resources"]) {
        self.siteURL = value[0][@"site"][@"site_url"];
    }
    if ([key isEqualToString:@"comic_cover"]) {
        self.BookIconOtherURL = value;
    }
    
}

- (void)calculateImageSize
{
    SDWebImageManager *manager = [[SDWebImageManager alloc]init];
    [manager downloadImageWithURL:[NSURL URLWithString:self.BookIconOtherURL] options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
        NSLog(@"%@", [NSThread currentThread]);
        self.image = image;
        self.imageSize = self.image.size;
        [[NSNotificationCenter defaultCenter]postNotificationName:@"calculateSize" object:nil];
    }];
    
//    dispatch_async(dispatch_get_global_queue(0, 0), ^{
//
//    self.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.BookIconOtherURL]]];
//    self.imageSize = self.image.size;
//    });
    
}

@end
